package com.ea.report.service.impl;

import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import com.ea.report.mapper.FileInfoMapper;
import com.ea.report.pojo.po.FileInfo;
import com.ea.report.service.FileService;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;

@Service
public class FileServiceImpl implements FileService {
    @Resource
    FileInfoMapper fileInfoMapper;

    private String getPathByType(String type) {
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        String tempPath = jarF.getParentFile().toString();
        if (!Objects.equals(tempPath, File.separator)) {
            tempPath += File.separator;
        }
        String rootPath = tempPath + "maven";
        switch (type) {
            case "jpeg":
            case "png":
            case "jpg":
                return rootPath + File.separator + "upload"
                        + File.separator + "user" + File.separator
                        + "photo" + File.separator;
            case "avi":
            case "h264":
            case "mp4":
            case "webm":
                return rootPath + File.separator + "upload"
                        + File.separator + "user" + File.separator
                        + "video" + File.separator;
            case "xlsx":
            case "xls":
            case "zip":
            case "tar":
            case "gz":
            case "rar":
            case "7z":
                return rootPath + File.separator + "upload"
                        + File.separator + "other" + File.separator
                        + type + File.separator;
            default:
                throw new BizException(ErrorCode.USER_PARAM_ERROR_FILE_NOT_SUPPORT);
        }
    }

    public MultipartFile convertFileToMultipartFile(File file) throws IOException {
        FileInputStream inputStream;
        inputStream = new FileInputStream(file);
        return new MockMultipartFile(
                "file",
                file.getName(),
                "APPLICATION_OCTET_STREAM",
                inputStream);
    }

    @Override
    public MultipartFile getMultipartFileByMd5(String md5) {
        try {
            FileInfo fileInfo = getFileInfoByMd5(md5);
            String type = fileInfo.getType();

            // 获取合并后的文件，转换成 MultipartFile，调用上传接口
            File file = new File(getPathByType(type) + md5 + File.separator + md5 + "." + type);

            // 设置数据库为已使用
            fileInfoMapper.saveFile(md5, file.length(), 0, 0);

            return convertFileToMultipartFile(file);
        } catch (
                FileNotFoundException e) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_FILE_NOT_EXISTS);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private FileInfo getFileInfoByMd5(String md5) {
        FileInfo fileInfo = fileInfoMapper.selectById(md5);
        if (fileInfo == null) {
            throw new BizException(ErrorCode.SYSTEM_RESOURCE_ERROR_FILE_NOT_EXISTS);
        }
        return fileInfo;
    }

}
